#ifndef ACTIONONDIRECTORY_HPP
#define ACTIONONDIRECTORY_HPP

#include <QDir>
#include "PageAction.hpp"
#include <vector>
#include <QFileInfo>
#include <QTextStream>

#include <iostream>

#include <DoQuMeasures/Exceptions.hpp>

template<class U>
class ActionOnDirectory
{
public:


    ActionOnDirectory(/*const QDir *pdir = NULL*/)
        : dirID(0), headerWritten(false)
    {
        _file = new QFile("outVerso2.csv");
          if (!_file->open(QIODevice::WriteOnly | QIODevice::Text))
              throw Exceptions::IOException("out.csv could not be openned");
    }

    ActionOnDirectory(const QString &dir)
    {
        //init(QDir(dir));
    }

    virtual ~ActionOnDirectory()
    {
        delete _out;
        delete _file;
    }

    void addAction(ActionOnFile<U> *action)
    {_actions.push_back(action);}

    void writeActions()
    {
        QTextStream writer(_file);
        writer << "dirPath" << ";" << "dirID" << ";" << "fileName" << ";" << "niveauDeTrans" << ";" ;
        foreach(ActionOnFile<U> * action,_actions)
        {
            writer << action->getActionName() << ";";
        }
        writer << "\n";
        headerWritten = true;
    }

    std::vector<U> doAction(const QDir &dir)
    {
        dirID++;
        if(!headerWritten) writeActions();
        QTextStream writer(_file);

        std::vector<QFileInfo>  ocrFiles;

        foreach(QFileInfo f, dir.entryInfoList(QStringList("image*"),QDir::Files,QDir::Name))
            ocrFiles.push_back(f);

        std::cout << "Action on Dir :" << dir.absolutePath().toStdString() << std::endl;


        std::vector<U> obs;
        std::vector<QFileInfo>::iterator it = ocrFiles.begin();
        std::vector<QFileInfo>::iterator itEnd = ocrFiles.end();

        int nb=0;
        for(; it != itEnd; it++)
        {
            QFileInfo f = (*it);



            std::cout << "Action on File :" << f.absoluteFilePath().toStdString()<< std::endl;
            nb++;
            writer << dir.absolutePath() << ";" << dirID << ";" << f.baseName() << ";" <<  nb << ";" ;
            foreach(ActionOnFile<U> * action,_actions)
            {

                U u= U(action->doAction(f));
                std::cout << u << std::endl;
                obs.push_back(u);

                writer << u << ";";
            }
            writer << "\n" ;
        }
        return obs;
    }

protected :

        QVector< ActionOnFile<U> *> _actions;
        QStringList _filtres;
        QFile *_file;
        QTextStream *_out;
        int dirID;
        bool headerWritten;




};

template<class U>
class ActionOnDirectories
{
public:
    typedef std::vector<U> VectorOfU;

    ActionOnDirectories(const QDir &pdir)
    {
        init(pdir);
    }

    ActionOnDirectories(const QString &dir)
    {
        init(QDir(dir));
    }

    void addAction(ActionOnDirectory<U> *action)
    {_actions.push_back(action);}

    std::vector<VectorOfU> doAction()
    {

        //qDebug() << _ocrFiles.size();
        std::vector< VectorOfU > obs;
        std::vector<QDir>::iterator it = _ocrFiles.begin();
        std::vector<QDir>::iterator itEnd = _ocrFiles.end();

        for(; it != itEnd; it++)
        {
            QDir f = (*it);
            std::cout << "Faire action on " << f.absolutePath().toStdString() << std::endl;
            foreach(ActionOnDirectory< U  > * action,_actions)
            {
                obs.push_back(action->doAction(f));
            }
        }
        return obs;
    }



protected :

        void init(const QDir &dir)
{
    foreach(QFileInfo f, dir.entryInfoList(QStringList("*.doc"),QDir::Dirs,QDir::Name)){
        _ocrFiles.push_back(f.absoluteFilePath());
        std::cout << f.absoluteFilePath().toStdString() << std::endl;
    }
    std::cout << "Initialisation OK" << std::endl;
}

protected :

        QVector< ActionOnDirectory< U > *> _actions;

std::vector<QDir>  _ocrFiles;


};



#endif // ACTIONONDIRECTORY_HPP
